package com.wen.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wen.common.R;
import com.wen.pojo.Consumer;
import com.wen.pojo.dto.ConsumerDto;
import com.wen.common.R;
import com.wen.pojo.Consumer;
import com.wen.pojo.Product;
import com.wen.service.ConsumerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/consumer")
public class ConsumerController {

    @Autowired
    private ConsumerService service;

    @PostMapping("/sel/{pageNum}/{pageSize}")
    public R sel(@RequestBody ConsumerDto dto,@PathVariable Long pageNum,@PathVariable Long pageSize){

//         Map<String, Object> columnMap = new HashMap<>();
//        columnMap.put("status", "s001-1");
//        List<Consumer> consumers = service.listByMap(columnMap);

        Page<Consumer> page = service.queryByPage(dto, pageNum, pageSize);

        return R.ok().data("records",page.getRecords()).
                data("total",page.getTotal()).
                data("current",page.getCurrent()).
                data("size",page.getSize());
    }

    @PostMapping("del")
    public R del(String id){
        System.out.println(id);
        boolean b = service.update().setSql("status = 's001-2'").eq("consumer_id",id).update();
        if (b)
            return R.ok().message("删除成功");
        return R.ok().message("删除失败");
    }

    @GetMapping("selById")
    public R selById(String id){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("consumer_id", id);
        List<Consumer> consumers = service.listByMap(columnMap);
        return R.ok().data("selById",consumers);
    }

    @PostMapping("up")
    public R up(@RequestBody Consumer consumer){
        UpdateWrapper<Consumer> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("consumer_id",consumer.getConsumerId());
        boolean b = service.update(consumer, updateWrapper);
        if (b)
            return R.ok().message("修改成功");
        return R.ok().message("修改失败");
    }

    /**
     * 订单管理模块（订单查询中的下单用户详情）
     * @param name
     * @return
     */
    @GetMapping("selByName")
    public R selByName(String name){
        Map<String, Object> columnMap = new HashMap<>();
        columnMap.put("login_name", name);
        List<Consumer> consumers = service.listByMap(columnMap);
        return R.ok().data("selByName",consumers);
    }

    @Autowired
    private ConsumerService consumerService;



    /**
     * 根据id查询用户的信息
     * @param id
     * @return
     */
    @GetMapping("/consumerGetId/{id}")
    public R getById(@PathVariable String id){
        Consumer selectById = service.getById(id);
        return R.ok().data("selectById",selectById);
    }





}
